package com.dingup.homework.controller;

import com.dingup.api.Util.RedisUtils;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.page.PageData;
import com.dingup.homework.bean.DO.HwHomeworkTeacherCorrectDO;
import com.dingup.homework.bean.DTO.TeacherCorrectDTO;
import com.dingup.homework.service.TeacherCorrectService;
import com.suyeer.basic.util.BHttpResUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/11/8.
 */
@RestController
@RequestMapping("/correct")
public class TeacherCorrectController {

    @Autowired
    private TeacherCorrectService teacherCorrectService;

    @RequestMapping(value = "/get_correct_list.do", method = RequestMethod.POST)
    public void getUnCorrectList(HttpServletRequest request,
                                 HttpServletResponse response,
                                 String query,
                                 Boolean ifCorrect,
                                 Integer page,
                                 Integer rows) {
        HttpResContent<PageData<TeacherCorrectDTO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取老师未批改作业列表");
        try {
            String teacherId = RedisUtils.getInstance().getUserId(request);
            PageData<TeacherCorrectDTO> res = teacherCorrectService.getTeacherCorrectList(teacherId, query, ifCorrect, page, rows);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }

    @RequestMapping(value = "/update_correct_teacher.do", method = RequestMethod.POST)
    public void updateCorrectTeacher(HttpServletRequest request,
                                     HttpServletResponse response,
                                     Integer id,
                                     String teacherId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改单个学生某次作业的批改老师");
        try {
            HwHomeworkTeacherCorrectDO correctDO = teacherCorrectService.getById(id);
            if (correctDO == null) {
                throw new Exception("当前批改记录不存在");
            }
            if (StringUtils.isEmpty(teacherId)) {
                throw new Exception("teacherId 为空");
            }
            teacherCorrectService.updateCorrectTeacher(id, teacherId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }
}
